package com.atcx.controller;


import com.atcx.pojo.Achievement;
import com.atcx.service.AchievementService;
import com.atcx.util.PageResult;
import com.atcx.util.QueryPageBean;
import com.atcx.util.Result;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pangShu
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/achievement")
public class AchievementController {
    private Result result;

    @Resource
    private AchievementService achievementService;
    //成绩报表
    @RequestMapping("getOneScore")
    public Result getOneScore(HttpServletRequest request, HttpServletResponse response){
        Integer id = getUId(request);
        Map<String, Object> oneScore = achievementService.getOneScore(id);
        if (oneScore!=null){
            result = new Result(true,"查询成绩成功",oneScore);

        }else {
            result = new Result(false,"查询信息失败");
        }
        return result;
    }
    //添加
    @RequestMapping("addAchievement")
    public Result addAchievement(@RequestBody Achievement achievement){
        System.out.println(achievement);
        int i = achievementService.addAchievement(achievement);
        if (i>0){
            result = new Result(true,"添加学生成绩成功");
        }else {
            result = new Result(false,"添加学生成绩失败");
        }
        return result;
    }
    //修改
    @RequestMapping("updateAchievement")
    public Result updateAchievement(@RequestBody Achievement achievement){
        int i = achievementService.updateAchievement(achievement);
        if (i>0){
            result = new Result(true,"修改学生成绩成功");
        }else {
            result = new Result(false,"修改学生成绩失败");
        }
        return result;
    }
    //删除
    @RequestMapping("deleteAchievement")
    public Result deleteAchievement(int id){
        int i = achievementService.deleteAchievement(id);

        if (i>0){
            result = new Result(true,"删除学生成绩成功");
        }else {
            result = new Result(false,"删除学生成绩失败,请先删除关联信息");
        }
        return result;
    }
    //查找
    @RequestMapping("findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = achievementService.findByPage(queryPageBean);
        if (page != null){
            result = new Result(true,"查询学生成绩成功",page);
        }else {
            result = new Result(false,"查询学生成绩失败");
        }
        return result;
    }
    //poi导出
    @RequestMapping("outputSheet")
    public Result outputSheet(){
        Boolean outputSheet = achievementService.outputSheet();
        if (outputSheet){
            result = new Result(true,"导出学生信息成功");
        }else {
            result = new Result(false,"导出学生信息失败");
        }
        return result;
    }
    public  static Integer getUId(HttpServletRequest request){
        Integer UserId = 0;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("userId")){
                String value = cookie.getValue();
                UserId = Integer.parseInt(value);
                break;
            }
        }
        return UserId;
    }
}

